package com.liuxin.mycontactdemo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.liuxin.mycontactdemo.R;
import com.liuxin.mycontactdemo.vo.Contact;

import java.util.List;

public class MyAdapter extends BaseAdapter {
    private Context context;
    private List<Contact> contact_datas;

    public MyAdapter(Context context, List<Contact> contact_datas) {
        this.context = context;
        this.contact_datas = contact_datas;
    }
    @Override
    public int getCount() {
        return contact_datas.size();
    }

    @Override
    public Object getItem(int position) {
        return contact_datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        final ViewHolder viewHolder;
        if(view==null) {
            view = LayoutInflater.from(context).inflate(R.layout.import_datalist, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.import_nameTv = view.findViewById(R.id.nameTv_import);
            viewHolder.import_phoneTv = view.findViewById(R.id.phoneTv_import);
            viewHolder.import_emailTv = view.findViewById(R.id.emailTv_import);
            viewHolder.importCb = view.findViewById(R.id.importCb);
            view.setTag(viewHolder);
        }else{
            viewHolder= (ViewHolder) view.getTag();
        }
        viewHolder.import_nameTv.setText(contact_datas.get(position).getName());
        viewHolder.import_phoneTv.setText(contact_datas.get(position).getPhone());
        viewHolder.import_emailTv.setText(contact_datas.get(position).getEmail());
        if(contact_datas.get(position).isChecked()){
            viewHolder.importCb.setChecked(true);
        }
        else{
            viewHolder.importCb.setChecked(false);
        }
        viewHolder.importCb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(viewHolder.importCb.isChecked()){
                    contact_datas.get(position).setChecked(true);
                }else{
                    contact_datas.get(position).setChecked(false);
                }
            }
        });
        viewHolder.importCb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(viewHolder.importCb.isChecked()){
                    contact_datas.get(position).setChecked(true);
                }
                else {
                    contact_datas.get(position).setChecked(false);
                }
            }
        });
        return view;
    }

    class ViewHolder{
        TextView import_nameTv,import_phoneTv,import_emailTv;
        CheckBox importCb;
    }
}
